﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CafeOnline.MvcFramework {
    public class SessionFactory {
        private const string SESSION_KEY = "__CafeOnline__SessionKey__";
        public static T GetSessionObject<T>() where T : class {
            return (T)HttpContext.Current.Session[SESSION_KEY + typeof(T).ToString()];
        }
        public static void SetObjectToSession<T>(T value) where T: class {
            HttpContext.Current.Session[SESSION_KEY + typeof(T).ToString()] = value;
        }
        public static void RemoveObjectFromSession<T>() {
            var item = (T) HttpContext.Current.Session[SESSION_KEY + typeof(T).ToString()];
            if (item != null && item is IDisposable) {
                ((IDisposable)item).Dispose();
            }
            HttpContext.Current.Session[SESSION_KEY + typeof(T).ToString()] = null;
        }
    }
}
